package com.spontaneappnew.adapters;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Typeface;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.spontaneappnew.Comment;
import com.spontaneappnew.R;

public class CommentsAdapter extends BaseExpandableListAdapter {
	public ArrayList<Comment> commentList;
	private Context _context;
    
    public CommentsAdapter(Context context, ArrayList<Comment> commentList) {
    	   this._context = context;
		   this.commentList = commentList;
    }
    

	@Override
	public Comment getChild(int groupPosition, int childPosition) {
		return this.commentList.get(groupPosition);
	}

	 @Override
	public long getChildId(int groupPosition, int childPosition) {
	        return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
 
        final String childText = (getChild(groupPosition, childPosition)).getComment();
        
 
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.comment_row, null);
        }
 
        TextView commentText = (TextView) convertView.findViewById(R.id.comment_row_comment);
        if (getChild(groupPosition, childPosition).getComment().compareTo("")==0){
        	commentText.setText(R.string.no_comment);
        	return convertView;
        }
 
        commentText.setText(childText);
        return convertView;
    }
 

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Comment getGroup(int groupPosition) {
		return this.commentList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return this.commentList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
		
        CharSequence headerTitle = DateUtils.getRelativeTimeSpanString(getGroup(groupPosition).getDate().getTime());
        final double rating  = getGroup(groupPosition).getRating();
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.comment_header, null);
        }
 
        TextView commentHeader = (TextView) convertView
                .findViewById(R.id.comment_header_heading);
        RatingBar _rating  = (RatingBar) convertView.findViewById(R.id.comment_header_rating);
        _rating.setRating((float)rating);
        commentHeader.setTypeface(null, Typeface.BOLD);
        commentHeader.setText(headerTitle);
 
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }
 
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
